package com.example.demo.config.shiro;


import com.example.demo.utils.RedisUtil;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;

/**
 * 重写Shiro 中的缓存
 * @param <K>
 * @param <V>
 */
//@Component
public class ShiroCache<K,V> implements Cache<K,V> {







    /**
     * 获取Cache
     * @param k
     * @return
     * @throws CacheException
     */
    @Override
    public V get(K k) throws CacheException {
        System.out.println("ShiroCache get===key "+ k);
        if(k==null){
            return  null;
        }else {
            RedisTemplate redisTemplate=RedisUtil.get();

            return (V)redisTemplate.opsForValue().get(k);
        }

    }

    /**
     *
     * @param k
     * @param v
     * @return
     * @throws CacheException
     */
    @Override
    public V put(K k, V v) throws CacheException {
        System.out.println(k+"=ShiroCache=="+v);
//        System.out.println("redisUtils=="+redisUtil);
        RedisTemplate redisTemplate=RedisUtil.get();
//        System.out.println("redisTemplate=="+redisTemplate);
//        System.out.println(redisUtil==null);
        redisTemplate.opsForValue().set(k.toString(),v);
        return null;
    }

    @Override
    public V remove(K k) throws CacheException {
        RedisTemplate redisTemplate=RedisUtil.get();
        redisTemplate.delete(k);
        return null;
    }

    @Override
    public void clear() throws CacheException {
        RedisTemplate redisTemplate=RedisUtil.get();
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<K> keys() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }
}
